﻿#region Libraries

using System;
using System.Drawing;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

#endregion

namespace XYZ_Inventory
{
    public partial class ProductSales : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblStatus.Text = "";
            if (!IsPostBack)
            {
                LoadGridView();
            }
        }

        private void LoadGridView()
        {
            XYZDBContext dbContext = new XYZDBContext();
            GridView1.DataSource = dbContext.FinishedProductSales.Join(dbContext.FinishedProductInventories,
                x => x.ProductID, y => y.ProductID, (a, b) => new
                {
                    SalesID = a.SalesId,
                    a.DateAndTime,
                    Product = b.Name,
                    a.Quantity
                }).OrderByDescending(k => k.SalesID).ToList();
            GridView1.DataBind();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            LoadGridView();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            LoadGridView();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            XYZDBContext dbContext = new XYZDBContext();
            int salesId = Convert.ToInt32(e.Keys["SalesID"]);

            FinishedProductSales sale =
                dbContext.FinishedProductSales.Single(x => x.SalesId == salesId);
            dbContext.FinishedProductSales.DeleteObject(sale);
            dbContext.SaveChanges();

            int productId = sale.ProductID;
            int qty = sale.Quantity;
            FinishedProductInventory product = dbContext.FinishedProductInventories.Single(x => x.ProductID == productId);
            product.Stock = product.Stock + qty;
            dbContext.SaveChanges();

            LoadGridView();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            lblStatus.Text = "";
            FinishedProductSales sale = new FinishedProductSales
            {
                ProductID = Convert.ToInt32(ddlProduct.SelectedValue),
                DateAndTime = DateTime.Now,
                Quantity = Convert.ToInt32(txtQty.Text)
            };
            XYZDBContext dbContext = new XYZDBContext();

            int productId = Convert.ToInt32(ddlProduct.SelectedValue);
            FinishedProductInventory product = dbContext.FinishedProductInventories.Single(x => x.ProductID == productId);
            if (product.Stock - Convert.ToInt32(txtQty.Text) >= 0)
            {
                dbContext.FinishedProductSales.AddObject(sale);
                product.Stock = product.Stock - Convert.ToInt32(txtQty.Text);
                dbContext.SaveChanges();
                LoadGridView();
                txtQty.Text = "";
            }
            else
            {
                lblStatus.Text = "Product out of stock";
                lblStatus.ForeColor = Color.Red;
            }
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            XYZDBContext dbContext = new XYZDBContext();
            int salesId = Convert.ToInt32(e.Keys["SalesID"]);

            FinishedProductSales sale =
                dbContext.FinishedProductSales.Single(x => x.SalesId == salesId);
            int productId = sale.ProductID;
            int qty = sale.Quantity;

            GridViewRow row = GridView1.Rows[e.RowIndex];
            TextBox txtQuantity = (TextBox) row.FindControl("txtQty");
            int newQty = Convert.ToInt32(txtQuantity.Text);
            sale.Quantity = newQty;

            FinishedProductInventory product = dbContext.FinishedProductInventories.Single(x => x.ProductID == productId);
            if (product.Stock + (qty - newQty) < 0)
            {
                lblStatus.Text = "This product is out of stock for your quantity";
                lblStatus.ForeColor = Color.Red;
            }
            else
            {
                product.Stock = product.Stock + (qty - newQty);
                dbContext.SaveChanges();

                GridView1.EditIndex = -1;
                LoadGridView();
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            LoadGridView();
        }
    }
}